angular.module("starter.controllers", [])
    .controller("HomeCtrl", ["$scope", 'datas', 'series', 'performances', 'contribution', 'salesSeries', '$timeout', function($scope, datas, series, performances, contribution, salesSeries, $timeout) {
        $scope.slideIndex = 0;
        $scope.slideHasChanged = function(index) {
            $scope.slideIndex = index;
            $scope.month = datas.times.length - 1;
        };

        // 时间
        $scope.times = datas.times;
        // 默认选中第一个时间
        $scope.month = datas.times.length - 1;
        // 获取排名
        $scope.getPres = function(month) {
            $scope.performances = performances.get(datas.times[month].id);
        };

        /** 综合比较-开始 **/
        $scope.type1 = 1;
        $scope.selectMonth = function(month, type) {
            $scope.type1 = type;
            load1(series.get(type, month));
        };

        function load1(data) {
            var xa = 0;
            var yb = 0;
            angular.forEach(data, function(v, k) {
                xa += v.data[0].x;
                yb += v.data[0].y;
            });
            $(function() {
                $("#container").highcharts({
                    chart: {
                        type: "bubble",
                        zoomType: "xy",
                        plotBorderWidth: 1,
                        gridLineDashStyle: 'solid'
                    },
                    legend: {
                        enabled: true,
                        align: "center",
                        backgroundColor: "#f2f2f2",
                        borderRadius: "5px",
                        itemMarginBottom: 5,
                        itemDistance: 10,
                        // padding: 5,
                        // symbolWidth: 10
                    },
                    exporting: {
                        enabled: false
                    },
                    credits: {
                        enabled: false
                    },
                    tooltip: {
                        valueDecimals: 2,
                        crosshairs: true,
                        shared: true,
                        pointFormat: '达成率：{point.x} % <br> 增长率：{point.y} % <br> 销量：￥{point.z} 万元'
                    },
                    // events: {
                    //     click: function() {},
                    //     selection: function() {}
                    // },
                    title: {
                        text: "",
                        floating: true
                    },
                    xAxis: {
                        title: {
                            text: "达成率(%)"
                        },
                        labels: {
                            format: "{value}"
                        },
                        plotLines: [{
                            color: "black",
                            dashStyle: "dot",
                            width: 2,
                            value: xa / 10,
                            label: {
                                rotation: 0,
                                y: 65,
                                style: {
                                    fontStyle: "italic"
                                },
                            },
                            zIndex: 3
                        }]
                    },
                    yAxis: {
                        startOnTick: true,
                        endOnTick: true,
                        title: {
                            text: "增长率(%)"
                        },
                        labels: {
                            format: "{value}"
                        },
                        maxPadding: 0.2,
                        plotLines: [{
                            color: "black",
                            dashStyle: "dot",
                            width: 2,
                            value: yb / 10,
                            label: {
                                align: "right",
                                style: {
                                    fontStyle: "italic"
                                },
                                x: -10
                            },
                            zIndex: 3
                        }]
                    },
                    plotOptions: {
                        series: {
                            dataLabels: {
                                enabled: true,
                                color: 'blank',
                                align: 'center',
                                style: {
                                    "fontSize": '8px'
                                },
                                format: "{point.name}"
                            }
                        }
                    },
                    series: data
                });
            });
        }

        $timeout(function() {
            load1(series.get($scope.type1, $scope.month));
        }, 200);
        

        /** 综合比较-结束 **/

        function load2(data) {
            $(function() {
                $('#container2').highcharts({
                    chart: {
                        type: 'pie',
                        options3d: {
                            enabled: true,
                            alpha: 10,
                            viewDistance: 100
                        }
                    },
                    legend: {
                        enabled: true,
                        align: "center",
                        // backgroundColor: "#f2f2f2",
                        borderRadius: "5px",
                        itemMarginBottom: 5,
                        itemDistance: 10,
                        // padding: 5,
                        // symbolWidth: 10
                    },
                    exporting: {
                        enabled: false
                    },
                    credits: {
                        enabled: false
                    },
                    title: {
                        text: '200万',
                        align: 'center',
                        verticalAlign: 'middle',
                        y: -37,
                        style: {
                            "fontSize": "16px"
                        }
                    },
                    // subtitle: {
                    //     text: '2016全年',
                    //     // style: {
                    //     //     // color: '#FF00FF',
                    //     //     fontWeight: 'bold',
                    //     //     fontSize: '12px'
                    //     // }
                    // },
                    plotOptions: {
                        pie: {
                            innerSize: 100,
                            depth: 1,
                            format: '<b>{point.name}</b>: {point.percentage} %',
                            allowPointSelect: true,
                            cursor: 'pointer',
                            dataLabels: {
                                enabled: false
                            },
                            showInLegend: true
                        },
                        // line: {
                        //     "lineWidth": 10
                        // },
                        // column: {
                        //     depth: 10
                        // }
                    },
                    series: [{
                        name: '团队成员销售贡献比例',
                        data: data
                    }]
                });
            });
        }

        $scope.type2 = 1;
        load2(contribution.get($scope.month, $scope.type2));


        $scope.loadMonth = function(month, type2) {
            $scope.type2 = type2;
            load2(contribution.get(month, type2));
        };


        //  团队ch

        /** 获取销量趋势 有单双月 **/
        // 默认请求单月  单月类型为1   双月类型为2
        $scope.salesType = 1;
        $scope.salesSelectMonth = function(month, type) {
            $scope.salesType = type;
            salesLoad(salesSeries.get(type, month));
        };

        function salesLoad(data) {
            $(function() {
                $('#container3').highcharts({
                    chart: {
                        type: 'area',
                        spacingBottom: 30
                    },
                    legend: {
                        enabled: true,
                        align: "center",
                        backgroundColor: "#f2f2f2",
                        borderRadius: "5px",
                        itemMarginBottom: 5
                    },
                    exporting: {
                        enabled: false
                    },
                    credits: {
                        enabled: false
                    },
                    title: {
                        text: ''
                    },
                    // subtitle: {
                    //     text: '2016全年'
                    // },
                    xAxis: {
                        categories: ['1', '2', '3', '4', '5', '6', '7'],
                        tickmarkPlacement: 'on',
                        title: {
                            enabled: false
                        }
                    },
                    yAxis: {
                        title: {
                            text: '销量'
                        },
                        labels: {
                            formatter: function() {
                                return this.value + '万元';
                            },
                            x: -5,
                            style: {
                                "fontSize": "10px"
                            }
                        }
                    },
                    tooltip: {
                        // split: true,
                        // valueSuffix: '销量'
                        pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.percentage:.1f}%</b> ({point.y:,.0f} millions)<br/>',
                        split: false
                    },
                    plotOptions: {
                        area: {
                            stacking: 'normal',
                            lineColor: '#ffffff',
                            lineWidth: 1,
                            // showInLegend: false,
                            fillOpacity: 0.5,
                            marker: {
                                lineWidth: 1,
                                lineColor: '#ffffff',
                                enabled: false
                            }
                        }
                    },
                    series: data
                });
            });
        }
        salesLoad(salesSeries.get($scope.salesType, $scope.month));
    }])
    .controller('RankingCtrl', ['$scope', 'ranks', '$ionicModal', '$ionicScrollDelegate', '$timeout', function($scope, ranks, $ionicModal, $ionicScrollDelegate, $timeout) {
        // $scope.datas = ranks.data;

        // 默认选中第一个
        $scope.index = 0;

        $scope.checkOne = function(id) {
            $scope.index = id;
            $scope.datas = ranks.get($scope.index);
        };

        $scope.checkTwo = function(id) {
            $scope.doctorRegistered.hide();
            $scope.datas1 = ranks.get($scope.index, id);
        };

        var urlDoctorRegistered = 'templates/doctorRegistered.html';
        $ionicModal.fromTemplateUrl(urlDoctorRegistered, {
            scope: $scope,
            animation: 'slide-in-right'
        }).then(function(doctorRegistered) {
            $scope.doctorRegistered = doctorRegistered;
        });

        // 弹出二级菜单
        $scope.popup = function() {
            // 先获取二级菜单的数据
            $scope.datas = ranks.get($scope.index);
            $scope.doctorRegistered.show();
        };
        $scope.datas1 = ranks.get($scope.index, 0);

        // 点击展开
        $scope.an = function(index) {
            // angular.forEach($scope.datas1, function(v, k) {
            //     v.istrue = false;
            //     if (k == index) {
            //         v.istrue = true;
            //     }
            // });
            $scope.datas1[index].istrue = !$scope.datas1[index].istrue;
            $timeout(function() {
                $ionicScrollDelegate.resize();
            }, 300);
        };
    }])
    .controller('ReportCtrl', ['$scope', function($scope) {}])
    .controller('MineCtrl', ['$scope', 'datas', 'channels', function($scope, datas, channels) {
        // 时间
        $scope.times = datas.times;
        // 默认选中第一个时间
        $scope.month = datas.times.length - 1;

        function loadChan(data) {
            $('#pie_channel').highcharts({
                chart: {
                    type: 'pie'
                },
                exporting: {
                    enabled: false
                },
                credits: {
                    enabled: false
                },
                title: {
                    text: ''
                },
                tooltip: {
                    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: true,
                            color: '#000000',
                            connectorColor: '#000000',
                            distance: 0,
                            format: '{point.percentage:.2f} %'
                        },
                        showInLegend: false // true
                    }
                },
                legend: {
                    layout: 'vertical',
                    align: 'left',
                    verticalAlign: 'top',
                    borderWidth: 0,
                    labelFormatter: function() {
                        return this.name + '&nbsp';
                    },
                    useHTML: true
                },
                series: data
            });
        }

        $scope.loadChannelByMonth = function(month) {
            $scope.datas = channels.get(month);
            loadChan($scope.datas);
        };

        $scope.loadChannelByMonth($scope.month);

    }]);
// .controller("ChatsCtrl",["$scope","Chats",function($scope,Chats){$scope.chats=b.all();$scope.remove=function(c){b.remove(c)}}])
// .controller("ChatDetailCtrl",["$scope","$stateParams","Chats",function($scope,$stateParams,Chats){$scope.chat=Chats.get($stateParams.chatId)}])
// .controller("AccountCtrl",["$scope",function($scope){$scope.settings={enableFriends:true}}]);
